//
//  BusLineViewController.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BusLineViewController.h"
#import "BusLineCell.h"
#import "BusDataManager.h"
#import "BusLineDetailViewController.h"

@interface BusLineViewController ()

@end

@implementation BusLineViewController

-(void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"BusLineDetailNotination" object:nil];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showBusDetail:) name:@"BusLineDetailNotination" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)showBusDetail:(NSNotification *)not{

    NSDictionary *dic=[not object];
    [self showBusLineDetailWithLineDic:dic];
}

-(void)showBusLineDetailWithLineDic:(NSDictionary *)lineDic{

   // NSLog(@"lineDic %@",lineDic);
    UINavigationController *tempNavigationController=self.navigationController;
    self.tabBarController.selectedViewController=tempNavigationController;
    
    BusLineDetailViewController *busLine=[[BusLineDetailViewController alloc]initWithNibName:@"BusLineDetailViewController" bundle:nil];
    busLine.title=[lineDic objectForKey:@"lineName"];
    NSDictionary *dic=[[BusDataManager currentBusDataManager]queryBusAllStationWithLineID:[lineDic objectForKey:@"lineID"]];
    busLine.busLineInfo=lineDic;
    busLine.busStationInfo=dic;
    NSArray *tempViewControllers=[NSArray arrayWithObjects:self,busLine, nil];
    [self.navigationController setViewControllers:tempViewControllers animated:YES];
    [busLine release];
}

#pragma mark -UITableViewDataSource And UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [BusLineCell defaultHeight];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellString=@"bus line";
    BusLineCell *cell=[tableView dequeueReusableCellWithIdentifier:cellString];
    if (!cell) {
        cell=[[[BusLineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString] autorelease];
        cell.backgroundColor=[UIColor clearColor];
    }
    NSDictionary *dic=[self.list objectAtIndex:indexPath.row];
    cell.busImgView.image=[UIImage imageNamed:@"busLineClick.png"];
    cell.busLineLabel.text=[dic objectForKey:@"lineName"];
    cell.busInfoTextView.text=[dic objectForKey:@"lineInfo"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    NSDictionary *dic=[self.list objectAtIndex:indexPath.row];
    
    [self showBusLineDetailWithLineDic:dic];
}

#pragma mark - 

-(void)fuzzySearchWithWord:(NSString *)text{

    self.list=[[BusDataManager currentBusDataManager]fuzzyQueryBusLineListWithFuzzyWord:text];
}
@end
